home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5550 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  50 lines

  1. Path: zeus.rbi.informatik.uni-frankfurt.de!gna1pc
  2. From: Ferber@zoology.uni-frankfurt.de (Michael Ferber)
  3. Newsgroups: comp.lang.c++
  4. Subject: problems reading binary data from file
  5. Date: Thu, 01 Feb 96 04:38:03 GMT
  6. Organization: Uni Frankfurt
  7. Message-ID: <4f4c4s$69q@zeus.rbi.informatik.uni-frankfurt.de>
  8. NNTP-Posting-Host: gna1pc.zoologys.uni-frankfurt.de
  9. X-Newsreader: News Xpress Version 1.0 Beta #4
  10.  
  11. I have to read/write binary data from/to a file. Up to now I did this with
  12. fread and fwrite, but I want to switch to the fstream classes to avoid mixture
  13. of c and c++ code. Unfortunately the following code (the first part which 
  14. writes to temp.bin) writes characters not binaries. What am I doing wrong???
  15. The second part (which writes to test.bin) works as expected. I use Watcom 
  16. 10.0a. The code is a modiffied piece of the Watcom samples provided with the 
  17. compiler. 
  18. Thanks in advance
  19. Michael
  20.  
  21. #include <fstream.h>
  22. #include <sys\stat.h>
  23. #include <sys\types.h>
  24. #include <fcntl.h>
  25. #include <stdio.h>
  26.  
  27. void main( void ) 
  28. {
  29.  
  30.  
  31.     int     handle;
  32.  
  33.     handle = open( "temp.bin", ios::binary | ios::out , S_IRWXU );
  34.     fstream    test ( handle );
  35.     for(int k = 1; k <=1000; k++)
  36.     test << k;
  37.  
  38.     FILE *test2;
  39.     test2 = fopen("test.bin", "w+b");
  40.     for(int m = 1; m <=1000; m++)
  41.         fwrite(&m, sizeof(m),1,    test2);
  42. }
  43.  
  44. ------------------------------------------------------------------------------------
  45. || Dr. Michael Ferber                     ||
  46. || Universitaet Frankfurt                 ||"science moves,
  47. || Zoologisches Institut                  || but slowly, slowly ......"
  48. || email: Ferber@zoology.uni-frankfurt.de ||                      Tennyson
  49. ------------------------------------------------------------------------------------
  50.